
'''
Node里面的参数:

executable: 可执行程序
package: 被执行程序所属的功能包
name: 节点名称
namespace: 设置命名空间
exec_name: 设置程序标签
parameters: 设置参数
remappings: 实现话题重映射
arguments: 为节点传参
ros_arguments: 为节点传参
'''


import launch
import launch_ros
from launch import LaunchDescription
from launch_ros.actions import Node

#封装终端指令相关类
#from launch.actions import ExecuteProcess
#from launch.substitutions import FindExecutable

#参数声明与获取
#from launch.actions import DeclareLaunchArgument
#from launch.substitutions import LaunchConfiguration

#文件包含相关
#from launch.actions import IncludeLaunchDescription
#from launch.launch_description_sources import PythonLaunchDescriptionSource

#分组相关
#from launch_ros.actions import PushRosNamespace
#from launch.actions import GroupAction"

#事件相关
#from launch.event_handlers import OnProcessStart, OnProcessExit
#from launch.actions import ExecuteProcess, RegisterEventHandler,LogInfo

#获取功能包下share目录路径
from ament_index_python.packages import get_package_share_directory
import launch_ros.parameter_descriptions



def generate_launch_description():

    #获取urdf路径
    urdf_package_path = get_package_share_directory('urdf_display')
    urdf_path = urdf_package_path + '/urdf/go2.urdf'


    #获取保存的rviz文件路径
    rviz_path = urdf_package_path + '/rviz/go2.rviz'


    #为launch声明参数,方便用命令行修改
    declare_arg_mode_path = launch.actions.DeclareLaunchArgument(
        name='model',default_value=str(urdf_path),
        description='UDRF的绝对路径'#描述
    )

    #通过文件路径获取文件内容，生成新的参数，以供传入robot_node
    robot_description = launch_ros.parameter_descriptions.ParameterValue(
        launch.substitutions.Command(
            ['xacro ',launch.substitutions.LaunchConfiguration('model')]),
            value_type=str
    )

    #状态发布节点
    robot_node = Node(
        package="robot_state_publisher",     #功能包名称
        executable="robot_state_publisher",  #节点的可执行程序
        parameters=[{'robot_description':robot_description}]
        )     

    # 可视化节点条件选择
    joint_state_gui = Node(
        package='joint_state_publisher_gui',
        executable='joint_state_publisher_gui',
    )          
    
    #关节状态发布节点
    joint_node = Node(
        package="joint_state_publisher",     #功能包名称
        executable="joint_state_publisher",  #节点的可执行程序
        )        
    
    #rviz节点
    rviz_node = Node(
        package="rviz2",     #功能包名称
        executable="rviz2",  #节点的可执行程序
        arguments=['-d',rviz_path]  #加载rviz_path路径的rviz文件
        ) 

    return LaunchDescription([
        declare_arg_mode_path, #参数
        robot_node,
        joint_state_gui,
        joint_node,
        rviz_node
    ]) #自动生成launch文件的函数

